* Figure A.7, right panel (financial crisis in 2008)

bys ccode: egen BankNoDeva2008_total  = total(BankNoDeva2008)

* Remove episodes which feature large appreciations
replace BankNoDeva2008_total=0 if ccode=="DNK" | ccode=="CHE"
replace BankNoDeva2008 =0 if ccode=="DNK" | ccode=="CHE"

if 1==0{ // Run this part of the code to produce excel file with list of episodes
* Extract list of countries that in 2008 had a financial crisis but no a large devaluation
preserve
	keep if BankNoDeva2008_total==1  
	bys ccode: gen counter=_n
	keep if counter==1
	keep ccode countryname
	sort countryname
	export excel countryname ccode using "`1'/CountriesFinancial2008.xlsx", sh("Episodes") replace //first(var) 
restore
* Confirm that episodes are not large devaluations
preserve
	gen er_alt = 1/E5 // Rely on E5
	sort id year
	gen gER_alt = (er_alt - l.er_alt)/l.er_alt
	keep if BankNoDeva2008==1 & year==2008
	keep ccode countryname gER_alt
	sort countryname
	export excel countryname gER_alt ccode using "`1'/CountriesFinancial2008.xlsx", first(var) sh("Confirm") //replace
restore
}
* Construct Figure A.7, right panel
preserve
	keep if BankNoDeva2008_total==1
	keep if year>=2005
	gen normyear=year-2008
	foreach var in importshare2 {
	gen temp=`var' if normyear==-1
	by ccode, sort: egen base=max(temp)
	gen rel`var'=(`var'/base-1)*100
	drop temp base
	}
	collapse (mean)  relimportshare2, by(year)
	label var year "Year"
	label var relimportshare2 "Imported Input Share Growth"	
	graph set window fontface "Times"	
	tw (line relimportshare2 year, lc(blue) lw(thick)) ///
	, yline(0,lc(black)) xline(2008,lc(black) lpattern(dash))  xt("Year") yt("Percent Change") /// 
	graphregion(margin(l=0 r=1 b=0 t=0)) graphregion(color(white)) ///
	legend(off) scale(1.2) ///
	text(-2 2008.75 "Import" "Share", color(blue) size(small)) ///
	title(Financial Crises of 2008) ///
	xlab(2005(1)2011) 
	graph export "`1'/FA7_B.pdf", replace
restore
